import socket

if __name__ == '__main__':
    # 创建套接字
    tcp_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    tcp_server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)
    # 绑定端口号
    tcp_server.bind(("",8000))
    # 设置监听
    tcp_server.listen(128)
    # 循环等待请求
    while True:
        # 等待客户端请求
        new_socket,ip_port = tcp_server.accept()
        recv_data = new_socket.recv(1024 * 4)
        print(recv_data)
        # 打开文件，读取数据
        with open("static/index.html",'r') as f:
            file_data = f.read()

        # 响应行
        response_line = "HTTP/1.1 200 OK\r\n"
        # 响应头
        response_header = "server: PWS/1.0\r\n"
        # 空行
        response_empth_line = "\r\n"
        # 响应体
        response_body = file_data

        response = response_line + response_header + response_empth_line + response_body
        # 把数据封装成http响应报文数据
        response_data = response.encode("utf-8")
        # 发送相应数据
        new_socket.send(response_data)
        new_socket.close()
